<?php
function anime_view($id,$chuong_id=''){
	//Set cookie to anti grab content
	$vl=md5($chuong_id.date('Y-m-d'));
	$_SESSION['manga24h_com_valid_user']=$vl;
	//echo "abc";
	
	$time = time();
	$date = date('Y-m-d',  $time);	
			//Update View  --------
	$model=new Model();	
	$model->where = "id=".$id;
	$model->select="views,view_day,view_week,view_month,last_update_view";
	$model->getdata('anime');
	$resultIndexPost = $model->fetchOne();
   	
   	
   	/*
   	 *  Check to see if it's neccessary to update view fields to 0
   	 */
   	if(date('z', strtotime($resultIndexPost['last_update_view']) != date('z', $time))){
   		$time_views['view_day'] = 1;
   	}else{
   		
   		if(date('Y', strtotime($resultIndexPost['last_update_view'])) == date('Y', $time)){
   			$time_views['view_day'] = $resultIndexPost['view_day'] + 2;
   		}else{
   			$time_views['view_day'] = 1;
   		}
   	}

   	
   if( date('W', strtotime($resultIndexPost['last_update_view'])) != date('W', $time)){
   			$time_views['view_week'] = 1;
   }else{
	   	if(date('Y', strtotime($resultIndexPost['last_update_view'])) == date('Y', $time)){
	   		$time_views['view_week'] = $resultIndexPost['view_week'] + 2;
	   	}else{
	   		$time_views['view_week'] = 1;
	   	}
   } 
   
 if(date('m', strtotime($resultIndexPost['last_update_view'])) != date('m', $time)){
   		$time_views['view_month'] = 1;
   }else{
   		if(date('Y', strtotime($resultIndexPost['last_update_view'])) == date('Y', $time)){
   			$time_views['view_month'] = $resultIndexPost['view_month'] + 2;
   		}else{
	   		$time_views['view_month'] = 1;
	   	}
   }
   $time_views['views'] = $resultIndexPost['views'] + 2;	  
   $model->where = "id =".$id;
   $time_views['last_update_view'] = $date;
   	$model->update('anime', $time_views);
	//Update chapterk view
	
	$model->nonquery("update chapterk2 set solanxem=solanxem+2 where id=".$chuong_id);
	
}

function blog_view($id){
	$db=new db;
	$db->connect();
	$db->nonquery("update blog_post set view = view+1 where id=".intval($id));
	$db->close();	

}
function video_view($id){
	$db=new db;
	$db->connect();
	$db->nonquery("update blog_video set view = view+1 where id=".intval($id));
	$db->close();	

}

function check_user_cookie(){

	if(isset($_COOKIE['anime24h_vn_user'])){
		$user=$_COOKIE['anime24h_vn_user'];
		$hash=$_COOKIE['anime24h_vn_valid'];
	
		if($user){
			$db=new db;
			$db->connect();
			$user=mysql_real_escape_string($user);
			$db->query("select * from users where username='".$user."'");
			if($db->num_rows()>0){
				$data=$db->fetchOne();
				$hash2=md5(md5($data['ip_login'].$data['email']));
				$ip=get_ip();
				if($ip==$data['ip_login']&& $hash2==$hash){
					user_login_session($data,1);
				}else{
				//huy cookie nay di do ip da bi doi roi
					setcookie('truyencuatui_info_user','1',strtotime('-120 Hours'),"/");
				}
			}else{
				//huy cookie nay di
			setcookie('truyencuatui_info_user','1',strtotime('-120 Hours'),"/");
			}
		}
	
	}
}